package com.jdk.demo.io.so_lingerDemo.demo3;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws Exception {
		StringBuffer buffer = new StringBuffer(500000);
		for (int i = 0; i < 500000; i++) {
			buffer.append("1");
		}
		buffer.append("end");
		System.out.println("server填充完毕！");

		ServerSocket serverSocket = new ServerSocket(8888);
		Socket socket = serverSocket.accept();
		socket.setSendBufferSize(500000);
		// close被阻塞的时间就是发送缓冲区中的数据发送时间
		socket.setSoLinger(true, 10);

		OutputStream out = socket.getOutputStream();
		out.write(buffer.toString().getBytes());

		long begin = System.currentTimeMillis();
		long end = 0;
		System.out.println("C=" + begin);
		socket.close();
		end = System.currentTimeMillis();
		System.out.println("D=" + end);
		System.out.println("阻塞时间差=" + (end - begin));
		serverSocket.close();
		// 发送的数据500004，结果证明：在on=true, linger=10时，若数据量小，则close方法阻塞的时间就是传输数据的时间，并且数据被完整发送，正常进行4次挥手，并不发送rst标记
	}

}
